/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;

/**
 *
 * @author Phạm Huy Đức
 */
public class thrReceiveEmail implements Runnable {

   @Override
   public void run() {
      try {
         Thread.sleep(500);
      } catch (InterruptedException ex) {
         Logger.getLogger(thrProcessReportText.class.getName()).log(Level.SEVERE, null, ex);
      }
      System.out.println("Run thrReceiveEmail!");
      try {
         doit("pop.gmail.com", "timesheet@telsoft.com.vn", "timesheet123");
      } catch (MessagingException ex) {
         Logger.getLogger(thrReceiveEmail.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
         Logger.getLogger(thrReceiveEmail.class.getName()).log(Level.SEVERE, null, ex);
      }

   }

   public static void doit(String HOST, String USERNAME, String PASSWORD) throws MessagingException, IOException {
      //Hàm nhận thông tin qua giao thức POP3
      Folder folder = null;
      Store store = null;
      try {
         Properties props = new Properties();
         props.put("mail.store.protocol", "pop3s"); // Google uses POP3S not POP3
         Session session = Session.getDefaultInstance(props);
         // session.setDebug(true);
         store = session.getStore();
         store.connect(HOST, USERNAME, PASSWORD);
         //Kết nối và lấy thông ti từ INBOX
         folder = store.getDefaultFolder().getFolder("INBOX");
         //Chỉ đọc không ghi
         folder.open(Folder.READ_ONLY);
         //Lấy danh sách tin nhắn
         Message[] messages = folder.getMessages();
         System.out.println("No of Messages : " + folder.getMessageCount());
         System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
         for (int i = 0; i < messages.length; ++i) {
            System.out.println("MESSAGE #" + (i + 1) + ":");
            Message msg = messages[i];
            String from = "unknown";
            if (msg.getReplyTo().length >= 1) {
               from = msg.getReplyTo()[0].toString();
            } else if (msg.getFrom().length >= 1) {
               from = msg.getFrom()[0].toString();
            }
            //Lấy tiêu đề
            String subject = msg.getSubject();
            System.out.println("Saving ... " + subject + " " + from);
            // you may want to replace the spaces with "_"
            // the files will be saved into the TEMP directory
            String filename = "c:/temp/" + subject;
//            saveParts(msg.getContent(), filename);
         }
      } finally {
         if (folder != null) {
            folder.close(true);
         }
         if (store != null) {
            store.close();
         }
      }
   }

   public static void saveParts(Object content, String filename)
           throws IOException, MessagingException {
      OutputStream out = null;
      InputStream in = null;
      try {
         if (content instanceof Multipart) {
            Multipart multi = ((Multipart) content);
            int parts = multi.getCount();
            for (int j = 0; j < parts; ++j) {
               MimeBodyPart part = (MimeBodyPart) multi.getBodyPart(j);
               if (part.getContent() instanceof Multipart) {
                  // part-within-a-part, do some recursion...
                  saveParts(part.getContent(), filename);
               } else {
                  String extension = "";
                  if (part.isMimeType("text/html")) {
                     extension = "html";
                  } else {
                     if (part.isMimeType("text/plain")) {
                        extension = "txt";
                     } else {
                        //  Try to get the name of the attachment
                        extension = part.getDataHandler().getName();
                     }
                     filename = filename + "." + extension;
                     System.out.println("... " + filename);
                     out = new FileOutputStream(new File(filename));
                     in = part.getInputStream();
                     int k;
                     while ((k = in.read()) != -1) {
                        out.write(k);
                     }
                  }
               }
            }
         }
      } finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.flush();
            out.close();
         }
      }
   }
}
